package com.senseauto.networkstatus;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(BuildConfig.VERSION_CODE>=23){
            if(checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED
            && checkSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED
            ){
                requestPermissions(new String[]{Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.INTERNET}, 10);
            }
        }
        Log.e(TAG, "网络isCarNetworkAvailable："+NetworkUtils.isCarNetworkAvailable(this));
        Log.e(TAG, "网络isNetworkAvailable："+NetworkUtils.isNetworkAvailable(this));
        new Thread(){
            @Override
            public void run() {
                super.run();
                boolean flag = NetworkUtils.isNetworkOnline();
                Log.e(TAG, "ping的方式 new Thread->isNetworkOnline:"+flag);
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                super.run();
                boolean flag = NetworkUtils.hasNetworkConnection(MainActivity.this.getApplicationContext());
                Log.e(TAG, "socket的方式 new Thread->hasNetworkConnection:"+flag);
            }
        }.start();
        //主线程网络请求会报错
        /*boolean flag = NetworkUtils.hasNetworkConnection(MainActivity.this.getApplicationContext());
        Log.e(TAG, "socket的方式 主线程 new Thread->hasNetworkConnection:"+flag);*/

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull  String[] permissions, @NonNull  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}